package com.genia.toolbox.projects.csv.business.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.projects.csv.bean.ConcreteFieldDescriptor;
import com.genia.toolbox.projects.csv.bean.impl.FieldDescriptorVisitorAdapter;
import com.genia.toolbox.projects.csv.business.manager.FieldCommentsGenerator;
import com.genia.toolbox.projects.csv.business.util.EnumerationUtils;

/**
 * {@link com.genia.toolbox.projects.csv.bean.FieldDescriptorVisitor} that
 * allows to generated needed comments for the Csv file.
 */
public class FieldCommentsGeneratorImpl
    extends FieldDescriptorVisitorAdapter
    implements FieldCommentsGenerator
{

  /**
   * the generated comment so far.
   */
  private final ThreadLocal<List<String>> comments = new ThreadLocal<List<String>>();

  /**
   * the {@link EnumerationUtils} to use.
   */
  private EnumerationUtils enumerationUtils;



  /**
   * clear the setted values to prevent memory leak.
   */
  public void clear()
  {
    comments.remove();
  }



  /**
   * visit method for {@link ConcreteFieldDescriptor}.
   * 
   * @param field
   *          the field to visit
   * @see com.genia.toolbox.projects.csv.bean.impl.FieldDescriptorVisitorAdapter#visitConcreteField(com.genia.toolbox.projects.csv.bean.ConcreteFieldDescriptor)
   */
  @Override
  public void visitConcreteField(ConcreteFieldDescriptor field)
  {
    String comment = null;

    // construct comment
    Class<?> fieldClass = field.getFieldClass();

    if (fieldClass.isEnum()) {
      comment = field.getColumnName() + " (";
      @SuppressWarnings("unchecked")
      List<String> values = getEnumerationUtils().getAllMessageValues(field.getFolderDescriptor().getMappingDescriptor().getMainLocale(), (Class<? extends Enum>) fieldClass);
      for (int i = 0; i < values.size(); i++) {
        comment = comment + values.get(i);
        if (i < values.size() - 1) {
          comment += " , ";
        }

      }
      comment += ")";
    }

    else {
      switch (field.getFieldType()) {
        case BOOLEAN:
          comment = field.getColumnName() + " (true , false)";
          break;
        case DATE:
          comment = field.getColumnName() + " (" + field.getFolderDescriptor().getMappingDescriptor().getDateFormat().toPattern() + ")";
          break;
        case DATETIME:
          comment = field.getColumnName() + " (" + field.getFolderDescriptor().getMappingDescriptor().getDatetimeFormat().toPattern() + ")";
          break;
        case TIME:
          comment = field.getColumnName() + " (" + field.getFolderDescriptor().getMappingDescriptor().getTimeFormat().toPattern() + ")";
          break;
        case FILE:
          comment = field.getColumnName() + " (real file name[|virtual file name[|content type]])";
          break;
        case DIRECTORY:
          comment = field.getColumnName() + " (zip file name)";
      }
    }
    if (comment != null) {
      getComments().add(comment);
    }
  }



  /**
   * getter for the comments property.
   * 
   * @return the comments
   */
  public List<String> getComments()
  {
    List<String> res = comments.get();
    if (res == null) {
      res = new ArrayList<String>();
      comments.set(res);
    }
    return res;
  }



  /**
   * getter for the enumerationUtils property.
   * 
   * @return the enumerationUtils
   */
  public EnumerationUtils getEnumerationUtils()
  {
    return enumerationUtils;
  }



  /**
   * setter for the enumerationUtils property.
   * 
   * @param enumerationUtils
   *          the enumerationUtils to set
   */
  public void setEnumerationUtils(EnumerationUtils enumerationUtils)
  {
    this.enumerationUtils = enumerationUtils;
  }

}